/*
 *  Copyright (C) <2009>  Pavel Vakeš <pavel.vakes@gmail.com>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <messages_dialog.h>

#include <KDebug>
#include <Plasma/Svg>

using namespace Plasma;

MessagesDialog::MessagesDialog() : QWidget()
{
	updateColors();

	model = new QStandardItemModel();

	view = new MailCheckerView();

	ItemDelegate *delegate = new ItemDelegate();
	delegate->setRoleMapping(Plasma::Delegate::SubTitleRole, MessagesDialogItem::StatusRole);
    delegate->setRoleMapping(Plasma::Delegate::ColumnTypeRole, MessagesDialogItem::ScopeRole);
	delegate->setRoleMapping(Plasma::Delegate::SubTitleMandatoryRole, MessagesDialogItem::SubTitleMandatoryRole);
	view->setItemDelegate(delegate);

	view->setModel(model);
	view->setFocusPolicy(Qt::NoFocus);

	QVBoxLayout * layout = new QVBoxLayout(this);
	layout->setMargin(0);
	layout->setSpacing(0);
	layout->addWidget(view);

	setLayout(layout);

	view->setMinimumSize(400, 150);

//	connect(Plasma::Theme::defaultTheme(), SIGNAL(themeChanged()), this, SLOT(updateColors()));
}

MessagesDialog::~MessagesDialog()
{
	delete view;
}

void MessagesDialog::createAccountItems(const std::vector<MailChecker*> &mail_checkers)
{
	model->clear();

	QStandardItem * root_item = model->invisibleRootItem();

	for (unsigned int i = 0; i < mail_checkers.size(); ++i) {
		MessagesDialogItem * item = new MessagesDialogItem(mail_checkers[i]);

		root_item->appendRow(item);

		connect(mail_checkers[i], SIGNAL( actionChanged(MailChecker::Action) ), item, SLOT( setAction(MailChecker::Action) ));
		connect(mail_checkers[i], SIGNAL( timeToCheckChanged(int) ), item, SLOT( setTimeToCheck(int) ));
		connect(mail_checkers[i], SIGNAL( endCheckMessages(int) ), item, SLOT( setMessages(int) ));
		connect(mail_checkers[i], SIGNAL( endCheckError(QString) ), item, SLOT( setError(QString) ));

		connect(item, SIGNAL( checkNowClicked() ), mail_checkers[i], SLOT( startCheck() ));
		connect(item, SIGNAL( runMailClientClicked() ), mail_checkers[i], SLOT( runMailClient() ));
	}
}

void MessagesDialog::updateColors()
{
    KColorScheme colorTheme = KColorScheme(QPalette::Active, KColorScheme::View,Plasma::Theme::defaultTheme()->colorScheme());

    QPalette p = palette();
    p.setColor(QPalette::Window, Plasma::Theme::defaultTheme()->color(Plasma::Theme::BackgroundColor));
    setPalette(p);
}
